<!-- 登录dams 测试 自动化报表 -->
<template>
  <!-- <button>env dams : 自动化报表 请配置env:wms后在访问这个页面</button>F -->
  <div class="p-8 pt-5 custom-report min-h-[432px]">
    <!-- 查询报表 -->
    <div class="custom-report-forms">
      <el-form ref="SelectRef" :inline="true" label-position="top" :model="formQuery">
        <el-form-item v-for="(value, key) in formQuery" :key="key" :label="formQueryLabel[key]" :prop="key">
          <el-input v-model="formQuery[key]" size="small" />
        </el-form-item>
      </el-form>
      <div>
        <el-button size="small" type="primary" @click="searchReport">{{ translateTitle('Dams.CustomReport.查询') }}</el-button>
        <el-button size="small" @click="resetReport">{{ translateTitle('Dams.CustomReport.重置') }}</el-button>
      </div>
    </div>
    <!-- 操作报表 -->
    <div class="operate">
      <el-button type="primary" @click="addReport">{{ translateTitle('Dams.CustomReport.新增') }}</el-button>
      <el-button type="danger" @click="deleteReport">{{ translateTitle('Dams.CustomReport.删除') }}</el-button>
    </div>
    <!-- 报表table -->
    <div class="mainTable">
      <vxe-table
        ref="reportTable"
        border
        :data="reportList"
        height="auto"
        :row-config="{ isHover: true, isCurrent: true }"
        show-header-overflow
        show-overflow
        stripe
        @cell-dblclick="dbTable"
        @checkbox-all="selectChange"
        @checkbox-change="selectChange"
      >
        <vxe-column type="checkbox" width="60" />
        <vxe-column v-for="(item, index) in reportListTitle" :key="item" :field="reportListProps[index]" sortable :title="item" />
      </vxe-table>
    </div>
    <vxe-pager
      v-model:current-page="currentPage"
      v-model:page-size="pagesize"
      align="right"
      :page-sizes="[50, 100, 150, 200]"
      :total="total"
      @page-change="currentChange"
    />
    <!-- 弹窗 -->
    <VuiModal v-model="showModal" @closed="closeDialog">
      <template #title>{{ reportTitles[stepsActive] }}</template>
      <div class="modal-body overflow-hidden min-h-[400px]" :style="{ minWidth: '50vw', maxWidth: '70vw' }">
        <!-- 顶部steps进度条 -->
        <el-steps :active="stepsActive" class="steps" finish-status="success">
          <el-step v-for="title in reportTitles" :key="title" :title="title" />
        </el-steps>
        <div class="dialogContent">
          <!-- 基础配置 -->
          <div v-show="stepsActive === 0">
            <el-form
              ref="formRef"
              :inline="true"
              label-position="top"
              label-width="100px"
              :model="basicConfigForm"
              :rules="basicConfigRules"
              :style="{ maxHeight: '50vh', overflow: 'auto' }"
            >
              <el-form-item :label="translateTitle('Dams.CustomReport.报表编号')" prop="REPORT_ID">
                <el-input v-model="basicConfigForm.REPORT_ID" :disabled="isEditTable" />
              </el-form-item>
              <el-form-item :label="translateTitle('Dams.CustomReport.报表名称')" prop="REPORT_NAME">
                <el-input v-model="basicConfigForm.REPORT_NAME" />
              </el-form-item>
              <el-form-item :label="translateTitle('Dams.CustomReport.显示顺序')" prop="SEQ">
                <el-input v-model.number="basicConfigForm.SEQ" />
              </el-form-item>
              <el-form-item :label="translateTitle('Dams.CustomReport.打印模板编号')" prop="PRINT_NUMBER">
                <el-input v-model="basicConfigForm.PRINT_NUMBER" :placeholder="translateTitle('Dams.CustomReport.请输入打印模板编号')" />
              </el-form-item>
              <el-form-item :label="translateTitle('Dams.CustomReport.所属菜单')" prop="MENU_ID" style="width: 200px">
                <el-select v-model="basicConfigForm.MENU_ID" :disabled="isEditTable" @change="cascaderChange">
                  <el-option v-for="item in menuList" :key="item.ID" :label="item.MENU_NAME" :value="item.ID" />
                </el-select>
              </el-form-item>
              <el-form-item :label="translateTitle('Dams.CustomReport.备注')" prop="NOTE">
                <el-input v-model="basicConfigForm.NOTE" />
              </el-form-item>
              <el-form-item :label="translateTitle('Dams.CustomReport.是否导出')" prop="EXPORT_FLAG">
                <el-switch v-model="basicConfigForm.EXPORT_FLAG" size="large" />
              </el-form-item>
              <el-form-item :label="translateTitle('Dams.CustomReport.是否打印')" prop="PRINT_FLAG">
                <el-switch v-model="basicConfigForm.PRINT_FLAG" />
              </el-form-item>

              <el-form-item :label="translateTitle('Dams.CustomReport.SQL语句')" prop="SQL_DESC" style="width: 100%">
                <CodeEditor v-if="CodeEditorOpen" v-model="basicConfigForm.SQL_DESC" />
              </el-form-item>
            </el-form>
            <div class="footer">
              <el-button type="primary" @click="sqlTestClick">{{ translateTitle('Dams.CustomReport.SQL测试') }}</el-button>
              <el-button :disabled="isIntercept" type="primary" @click="nextStep(1)">{{ translateTitle('Dams.CustomReport.下一步') }}</el-button>
            </div>
          </div>
          <!-- 字段配置 -->
          <div v-show="stepsActive === 1">
            <div style="height: 60vh">
              <vxe-table
                border
                :data="fieldConfigList"
                height="auto"
                :row-config="{ isHover: true, isCurrent: true }"
                show-header-overflow
                show-overflow
                stripe
              >
                <vxe-column field="COLUMNNAME" sortable :title="translateTitle('Dams.CustomReport.字段名称')" />
                <vxe-column field="COLUMN_DESC" sortable :title="translateTitle('Dams.CustomReport.字段中文名称')">
                  <template #default="{ row }">
                    <vxe-input v-model="row.COLUMN_DESC" size="small" />
                  </template>
                </vxe-column>
              </vxe-table>
            </div>

            <div class="footer">
              <el-button type="primary" @click="backStep(0)">{{ translateTitle('Dams.CustomReport.上一步') }}</el-button>
              <el-button type="primary" @click="nextStep(2)">{{ translateTitle('Dams.CustomReport.下一步') }}</el-button>
            </div>
          </div>
          <!-- 查询配置 -->
          <div v-show="stepsActive === 2">
            <div style="height: 60vh">
              <vxe-table
                border
                :data="selectList"
                height="auto"
                :row-config="{ isHover: true, isCurrent: true }"
                show-header-overflow
                show-overflow
                stripe
              >
                <vxe-column field="COLUMNNAME" sortable :title="translateTitle('Dams.CustomReport.字段值')" />
                <vxe-column field="COLUMN_DESC" sortable :title="translateTitle('Dams.CustomReport.字段中文名称')">
                  <template #default="{ row }">
                    <vxe-input v-model="row.COLUMN_DESC" size="small" />
                  </template>
                </vxe-column>
                <vxe-column field="CONTROL_TYPE" sortable :title="translateTitle('Dams.CustomReport.查询控件')">
                  <template #default="{ row }">
                    <vxe-select v-model="row.CONTROL_TYPE" transfer>
                      <vxe-option v-for="item in querySelete" :key="item.value" :label="item.label" :value="item.value" />
                    </vxe-select>
                  </template>
                </vxe-column>
                <vxe-column field="CODE" sortable :title="translateTitle('Dams.CustomReport.映射系统代码')">
                  <template #default="{ row }">
                    <vxe-select v-if="row.CONTROL_TYPE === 'select'" v-model="row.CODE" transfer>
                      <vxe-option v-for="item in sysCodeSelect" :key="item.value" :label="item.DESCRC" :value="item.CODE" />
                    </vxe-select>
                  </template>
                </vxe-column>
                <vxe-column field="CHECKBOX_FLAG" sortable :title="translateTitle('Dams.CustomReport.是否支持多选')">
                  <template #default="{ row }">
                    <vxe-select v-model="row.CHECKBOX_FLAG" transfer>
                      <vxe-option v-for="item in isMultiple" :key="item.value" :label="item.label" :value="item.value" />
                    </vxe-select>
                  </template>
                </vxe-column>
              </vxe-table>
            </div>

            <div class="footer">
              <el-button type="primary" @click="backStep(1)">{{ translateTitle('Dams.CustomReport.上一步') }}</el-button>
              <el-button type="primary" @click="nextStep(3)">{{ translateTitle('Dams.CustomReport.下一步') }}</el-button>
            </div>
          </div>
          <!-- 报表生成 -->
          <div v-show="stepsActive === 3 || stepsActive === 4" class="successSet">
            <img alt="" src="@/assets/dams_images/u154.svg" />
            <p>{{ `${translateTitle('Dams.CustomReport.恭喜配置完成')}，${translateTitle('Dams.CustomReport.请进行菜单授权后查看')}` }}</p>
          </div>
        </div>
      </div>
    </VuiModal>
  </div>
</template>

<script>
  import { translateTitle } from '@/utils/i18n'
  import { processingConstant } from '@ve/utils/utils'
  import CodeEditor from '@/project/dams/components/codeEditor.vue'
  import { getList } from '@/project/dams/api/home'
  import { GFCD } from '@/utils/utils'
  import { mapGetters } from 'vuex'
  export default {
    name: 'Index',
    components: {
      CodeEditor,
    },
    data() {
      let numberValidator = (rule, value, callback) => {
        if (this.basicConfigForm.PRINT_FLAG && (!value || value === ' ')) {
          return callback(new Error(translateTitle('Dams.CustomReport.请输入打印模板编号')))
        } else {
          return callback()
        }
      }
      let IDValidator = async (rule, value, callback) => {
        try {
          let params = { '#LIMIT#': 1, '#PAGE_SIZE#': 9999, MENU_CODE: value, SUB_SYSTEM: this.appCode, ATTRIBUTION_CODE: 'PC' }

          let res = await getList(`${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`, {
            data: JSON.stringify(params),
            method: '*.configRecordMenu.query',
          })
          if (!this.isEditTable && res.data['*.configRecordMenu.query'].data.length > 0) {
            return callback(new Error(translateTitle('Dams.CustomReport.报表编号已存在')))
          } else if (!value || value === ' ') {
            return callback(new Error(translateTitle('Dams.CustomReport.请输入报表编号')))
          } else {
            return callback()
          }
        } catch (err) {
          console.log(err)
        }
      }
      return {
        CodeEditorOpen: true,
        showModal: false,
        ORG_ID: '',
        LANGUAGE_CODE: '',
        stepsActive: 0,
        currentPage: 1, //当前页
        pagesize: 50, //显示数量
        total: 0, //总条数
        isEditTable: false, //是否修改表格
        formQuery: {
          //查询报表
          REPORT_ID: '',
          REPORT_NAME: '',
        },
        //查询报表label
        formQueryLabel: { REPORT_ID: translateTitle('Dams.CustomReport.报表编号'), REPORT_NAME: translateTitle('Dams.CustomReport.报表名称') },
        deleteList: [], //删除的报表
        basicConfigForm: {
          PRINT_FLAG: 'N',
          SQL_DESC: '',
          DB_ID: '',
          MENU_ID: '',
        },
        dialogVisible: false,
        reportTitles: [
          translateTitle('Dams.CustomReport.基础配置'),
          translateTitle('Dams.CustomReport.字段配置'),
          translateTitle('Dams.CustomReport.查询配置'),
          translateTitle('Dams.CustomReport.报表生成'),
        ],
        reportList: [], //报表
        reportListTitle: [
          translateTitle('Dams.CustomReport.功能编号'),
          translateTitle('Dams.CustomReport.报表名称'),
          translateTitle('Dams.CustomReport.导出标记'),
          translateTitle('Dams.CustomReport.打印标记'),
          translateTitle('Dams.CustomReport.备注'),
          translateTitle('Dams.CustomReport.新增人'),
          translateTitle('Dams.CustomReport.新增时间'),
          translateTitle('Dams.CustomReport.修改人'),
          translateTitle('Dams.CustomReport.修改时间'),
        ], //报表Title
        reportListProps: ['REPORT_ID', 'REPORT_NAME', 'EXPORT_FLAG', 'PRINT_FLAG', 'NOTE', 'ADDWHO', 'ADDTIME', 'EDITWHO', 'EDITTIME'], //报表Props
        fieldConfigList: [],
        menuList: [], //菜单
        selectList: [],
        querySelete: [
          //查询控件
          {
            value: 'text',
            label: translateTitle('Dams.CustomReport.文本框'),
          },
          {
            value: 'date',
            label: translateTitle('Dams.CustomReport.时间框'),
          },
          {
            value: 'select',
            label: translateTitle('Dams.CustomReport.下拉框'),
          },
        ],
        sysCodeSelect: [], //映射系统代码
        isMultiple: [
          {
            value: 'Y',
            label: translateTitle('Dams.CustomReport.是'),
          },
          {
            value: 'N',
            label: translateTitle('Dams.CustomReport.否'),
          },
        ],
        selectField: [],
        whereField: [],
        basicConfigRules: {
          //基础设置的验证规则
          REPORT_NAME: [{ required: true, message: translateTitle('Dams.CustomReport.请输入报表名称'), trigger: 'blur' }],
          REPORT_ID: [{ validator: IDValidator, required: true, trigger: 'blur' }],
          SQL_DESC: [{ required: true, message: translateTitle('Dams.CustomReport.请输入SQL语句'), trigger: 'blur' }],
          MENU_ID: [{ required: true, message: translateTitle('Dams.CustomReport.请选择菜单'), trigger: 'change' }],
          SEQ: [
            { required: true, message: translateTitle('Dams.CustomReport.请输入显示顺序'), trigger: 'blur' },
            { type: 'number', message: translateTitle('Dams.CustomReport.请输入数字'), trigger: 'blur' },
          ],
          PRINT_NUMBER: [{ validator: numberValidator, trigger: 'blur' }],
        },
        isIntercept: true, //是否拦截下一步
        isDataChange: false, //基础报表是否被修改
        rowReportList: [], //没有被转换的报表
      }
    },

    computed: {
      ...mapGetters('user', ['appCode']),
    },
    watch: {
      basicConfigForm: {
        handler: function () {
          this.isDataChange = true
          this.isIntercept = true
        },
        deep: true,
      },
    },
    created() {
      this.getReportList(this.currentPage, this.pagesize)
      this.ORG_ID = processingConstant(this, '#ORG_ID#')
      this.LANGUAGE_CODE = processingConstant(this, '#LANGUAGE_CODE#')
    },
    methods: {
      translateTitle,
      //获取报表数据
      async getReportList(currentPage, pagesize) {
        let paramsData = JSON.parse(JSON.stringify(this.formQuery))
        paramsData['#LIMIT#'] = currentPage
        paramsData['#PAGE_SIZE#'] = pagesize
        paramsData.ORG_ID = this.ORG_ID
        let res = await getList(`${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`, {
          data: JSON.stringify(paramsData, '2'),
          method: '*.record.header.query',
        })
        if (res.code === '00000') {
          this.reportList = res.data['*.record.header.query'].data
          this.rowReportList = JSON.parse(JSON.stringify(this.reportList))
          this.total = res.data['*.record.header.query'].count
        } else {
          this.$message({
            type: 'info',
            message: res.msg,
          })
        }
      },
      //获取菜单数据
      async getMenuList() {
        //获取菜单数据
        try {
          let params = { '#LIMIT#': 1, '#PAGE_SIZE#': 9999, APP_CODE: this.appCode, SUB_SYSTEM: this.appCode, LANGUAGE_CODE: 'zh_CN' }
          let {
            data: { PC },
          } = await getList(`${GFCD('dams').domain}${GFCD('dams').baseUrl}`, {
            data: JSON.stringify(params),
            method: 'sysPageServiceImpl.standardPageQuery',
          })
          this.menuList = PC.first
        } catch (err) {
          console.log(err)
        }
      },
      //查询报表
      async searchReport() {
        this.getReportList(this.currentPage, this.pagesize)
      },
      //重置查询
      resetReport() {
        this.$refs.SelectRef.resetFields()
        this.getReportList(this.currentPage, this.pagesize)
      },
      //双击弹窗
      dbTable() {
        this.CodeEditorOpen = true
        let list = this.$refs.reportTable.getCurrentRecord()
        this.stepsActive = 0
        this.isEditTable = true
        this.getMenuList()
        this.showModal = true
        //未修改的当前行数据
        let currentRow = this.rowReportList.find((item) => {
          return item.REPORT_ID === list.REPORT_ID
        })
        list.EXPORT_FLAG = currentRow.EXPORT_FLAG === 'Y' ? true : false
        list.PRINT_FLAG = currentRow.PRINT_FLAG === 'Y' ? true : false
        this.basicConfigForm = list
      },
      //修改报表
      async editTable() {
        let queryForm = JSON.parse(JSON.stringify(this.basicConfigForm))
        queryForm.EXPORT_FLAG = this.basicConfigForm.EXPORT_FLAG ? 'Y' : 'N'
        queryForm.PRINT_FLAG = this.basicConfigForm.PRINT_FLAG ? 'Y' : 'N'
        this.$refs['formRef'].validate(async (valid) => {
          if (valid) {
            try {
              await getList(`${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`, {
                data: JSON.stringify(queryForm),
                method: '*.configRecord.update',
              })
              this.$message({
                type: 'success',
                message: translateTitle('Dams.CustomReport.修改成功'),
              })
            } catch (err) {
              console.log(err)
            }
          } else {
            return false
          }
        })
      },
      //新增
      async addReport() {
        this.CodeEditorOpen = true
        this.showModal = true
        this.isEditTable = false
        this.stepsActive = 0
        this.getMenuList()
      },
      //选择项
      selectChange() {
        let list = this.$refs.reportTable.getCheckboxRecords()
        this.deleteList = list.map((item) => {
          return {
            ORG_ID: item.ORG_ID,
            REPORT_ID: item.REPORT_ID,
            APP_CODE: this.appCode,
          }
        })
      },
      //删除
      deleteReport() {
        if (this.deleteList.length > 0) {
          this.$confirm(translateTitle('Dams.CustomReport.确定删除报表吗'), translateTitle('Dams.CustomReport.提示'), {
            confirmButtonText: translateTitle('Dams.CustomReport.确定'),
            cancelButtonText: translateTitle('Dams.CustomReport.取消'),
            type: 'warning',
          })
            .then(async () => {
              let arr = []
              this.deleteList.forEach((x) => {
                arr.push({ MENU_CODE: x.REPORT_ID, APP_CODE: this.appCode })
              })
              try {
                await getList(`${GFCD('dams').domain}${GFCD('dams').baseUrl}`, {
                  data: JSON.stringify(arr),
                  method: 'sysPageServiceImpl.standardDeleteSecondPage',
                })
                try {
                  await getList(`${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`, {
                    data: JSON.stringify(this.deleteList),
                    method: '*.configRecord.delete',
                  })
                  this.getReportList(this.currentPage, this.pagesize)
                } catch (err) {
                  console.log(err)
                }
              } catch (err) {
                console.log(err)
              }
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: translateTitle('Dams.CustomReport.已取消删除'),
              })
            })
        } else {
          this.$alert(translateTitle('Dams.CustomReport.请至少选择一项内容'), translateTitle('Dams.CustomReport.提示'), {
            confirmButtonText: translateTitle('Dams.CustomReport.确定'),
          })
        }
      },
      //上一步
      backStep(step) {
        this.stepsActive = step
        this.isIntercept = false
        this.isDataChange = false
      },
      //下一步
      async nextStep(step) {
        if (step === 1) {
          //验证模板编号是否存在
          if (this.basicConfigForm.PRINT_FLAG) {
            console.log(this.basicConfigForm.PRINT_NUMBER, 1111)
            // this.stepsActive = 0
            try {
              let paramsData = {
                '#LIMIT#': 1,
                '#PAGE_SIZE#': 50,
                ORG_ID: this.ORG_ID,
                BUSINESS_REPORT_CODE: this.basicConfigForm.PRINT_NUMBER,
                WAREHOUSE_ID: this.$store.getters['user/wareHoseId'],
              }
              let res = await getList(`${GFCD('wms').domain}${GFCD('wms').baseUrl}`, {
                data: JSON.stringify(paramsData),
                method: 'wms.report.check.query',
              })
              let count = res.data['wms.report.check.query'].count
              if (count === 0) {
                this.$message({
                  type: 'error',
                  message: translateTitle('Dams.CustomReport.打印模板编号不存在'),
                })
                return
              }
            } catch (e) {
              console.log(e)
            }
          }
          //编辑
          if (this.isEditTable) {
            this.editTable()
            //字段配置的回显
            try {
              let paramsData = { '#LIMIT#': 1, '#PAGE_SIZE#': 50, ORG_ID: this.ORG_ID, REPORT_ID: this.basicConfigForm.REPORT_ID }
              let res = await getList(`${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`, {
                data: JSON.stringify(paramsData),
                method: '*.configRecordDetails.query',
              })

              this.fieldConfigList = res.data['*.configRecordDetails.query'].data
              let newArr = []
              Object.keys(this.selectField).forEach((item) => {
                let key = this.selectField[item]
                let findVal = this.fieldConfigList.find((x) => {
                  return x.COLUMNNAME === key
                })
                if (findVal) {
                  newArr.push(findVal)
                } else {
                  newArr.push({ COLUMNNAME: key, COLUMN_DESC: '' })
                }
              })
              this.fieldConfigList = newArr
              this.stepsActive = step
            } catch (err) {
              console.log(err)
            }
          } else {
            //新增
            this.basicConfigForm.DB_ID = this.appCode
            let quertForm = JSON.parse(JSON.stringify(this.basicConfigForm))
            quertForm.OPERATION_SEQNO = '100'
            quertForm.ORG_ID = this.ORG_ID
            quertForm.EXPORT_FLAG = this.basicConfigForm.EXPORT_FLAG ? 'Y' : 'N'
            quertForm.PRINT_FLAG = this.basicConfigForm.PRINT_FLAG ? 'Y' : 'N'
            //验证通过并且数据被修改
            if (!this.isIntercept && this.isDataChange) {
              let checdMenu = this.menuList.find((item) => {
                return item.ID === this.basicConfigForm.MENU_ID
              })
              let data = {
                SORT: Number(this.basicConfigForm.SEQ),
                LANGUAGE_CODE: this.LANGUAGE_CODE,
                MENU_NAME: this.basicConfigForm.REPORT_NAME,
                MODEL_ID: this.basicConfigForm.REPORT_ID,
                SUB_SYSTEM: this.appCode,
                MENU_ICON: '',
                MENU_URL: 'views/customReportPage/index',
                MENU_SCODE: checdMenu.ID,
                MENU_LV: '2',
                TYPE: '0',
                PAGE_JSON: null,
                ATTRIBUTION_CODE: checdMenu.ATTRIBUTION_CODE,
                APP_CODE: this.appCode,
              }
              try {
                let { code } = await getList(`${GFCD('dams').domain}${GFCD('dams').baseUrl}`, {
                  data: JSON.stringify(data),
                  method: 'sysPageServiceImpl.standardAddSecondPage',
                })
                if (code === '00000') {
                  try {
                    let postUrl = `${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`
                    await getList(postUrl, { data: JSON.stringify(quertForm), method: '*.configRecord.save' })
                    this.stepsActive = step
                  } catch (err) {
                    this.isIntercept = true
                    this.stepsActive = 0
                    console.log(err)
                  }
                }
              } catch (err) {
                this.isIntercept = true
                this.stepsActive = 0
                console.log(err)
              }
            } else {
              this.stepsActive = step
            }
          }
        } else if (step === 2) {
          let newObj = {}
          for (let item in this.whereField) {
            newObj[item] = this.whereField[item]
          }
          if (this.isEditTable) {
            try {
              //查询表单回显
              let paramsData = { '#LIMIT#': 1, '#PAGE_SIZE#': 50, ORG_ID: this.ORG_ID, REPORT_ID: this.basicConfigForm.REPORT_ID }
              let res = await getList(`${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`, {
                data: JSON.stringify(paramsData),
                method: '*.configRecordWhere.query',
              })
              this.selectList = res.data['*.configRecordWhere.query'].data
              if (this.selectList.length > 0) {
                let arr = []
                Object.keys(newObj).forEach((item) => {
                  let key = newObj[item]
                  let findVal = this.selectList.find((x) => {
                    return x.QUERY_COLUMN === key
                  })
                  if (findVal) {
                    arr.push(findVal)
                  } else {
                    arr.push({
                      COLUMNNAME: newObj[item],
                      CHECKBOX_FLAG: '',
                      CODE: '',
                      COLUMN_DESC: '',
                      CONTROL_TYPE: '',
                    })
                  }
                  arr = JSON.parse(JSON.stringify(arr).replace(/QUERY_COLUMN/g, 'COLUMNNAME'))
                })
                this.selectList = arr
              } else {
                Object.keys(newObj).forEach((item) => {
                  this.selectList.push({
                    COLUMNNAME: newObj[item],
                    CHECKBOX_FLAG: '',
                    CODE: '',
                    COLUMN_DESC: '',
                    CONTROL_TYPE: '',
                  })
                })
              }
            } catch (err) {
              console.log(err)
            }
          } else {
            this.selectList = Object.keys(newObj).map((item) => {
              return { COLUMNNAME: newObj[item], COLUMN_DESC: '' }
            })
          }
          //获取映射系统代码数据
          let postUrl = `${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`
          let data = { '#LIMIT#': 1, '#PAGE_SIZE#': 9999, ORG_ID: this.ORG_ID }
          try {
            let getListData = await getList(postUrl, { data: JSON.stringify(data), method: '*.sys.paramater.query' })
            this.sysCodeSelect = getListData.data['*.sys.paramater.query'].data
          } catch (err) {
            console.log(err)
          }

          //提交报表配置字段
          this.fieldConfigList.forEach((x, i) => {
            x.ORG_ID = this.ORG_ID
            x.REPORT_ID = this.basicConfigForm.REPORT_ID
            x.NOTE = ''
            x.OPERATION_SEQNO = '100'
            x.LINENO = i + 1
          })
          try {
            await getList(`${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`, {
              data: JSON.stringify(this.fieldConfigList),
              method: '*.configRecordDetails.save',
            })
            this.stepsActive = step
          } catch (err) {
            console.log(err)
          }
        } else if (step === 3) {
          this.selectList.forEach((x) => {
            x.ORG_ID = this.ORG_ID
            x.REPORT_ID = this.basicConfigForm.REPORT_ID
            x.QUERY_COLUMN = x.COLUMNNAME
            x.NOTE = ''
            x.OPERATION_SEQNO = '100'
          })

          try {
            await getList(`${GFCD('dams').domain}${this.$store.getters['user/appCodeUrl']}`, {
              data: JSON.stringify(this.selectList),
              method: '*.configRecordQuery.save',
            })
            this.stepsActive = 4
          } catch (err) {
            this.stepsActive = 2
            console.log(err)
          }
        }
      },
      //关闭弹窗
      closeDialog() {
        this.CodeEditorOpen = false
        this.isIntercept = true
        this.$refs.formRef.resetFields()
        this.isEditTable = false
        this.getReportList(this.currentPage, this.pagesize)
        this.stepsActive = 0
        console.log(this.basicConfigForm)
      },
      //测试SQL语句
      sqlTestClick() {
        let data = {
          DB_ID: this.appCode,
          SQL_TXT: this.basicConfigForm.SQL_DESC,
        }
        //输入验证
        this.$refs['formRef'].validate(async (valid) => {
          if (valid) {
            this.isIntercept = false
            try {
              //SQL测试
              let getListData = await getList(`${GFCD('dams').domain}${GFCD('dams').baseUrl}`, {
                data: JSON.stringify(data),
                method: 'dams.customReport.sqlTest',
              })
              if (getListData.code === '00000') {
                //字段配置数据
                this.selectField = getListData.data.selectField
                //查询配置数据
                this.whereField = getListData.data.whereField
                let reportData = {}
                for (let item in this.selectField) {
                  reportData[item] = this.selectField[item]
                }
                this.fieldConfigList = Object.keys(reportData).map((item) => {
                  return { COLUMNNAME: reportData[item], COLUMN_DESC: '' }
                })
                this.$message({
                  type: 'success',
                  message: translateTitle('Dams.CustomReport.测试成功'),
                })
              }
            } catch (err) {
              this.isIntercept = true
              console.log(err)
            }
          } else {
            this.isIntercept = true
            return false
          }
        })
      },
      //菜单选择
      cascaderChange(value) {
        this.basicConfigForm.MENU_ID = value
      },
      currentChange({ currentPage, pageSize }) {
        this.currentPage = currentPage
        this.pagesize = pageSize
        this.getReportList(this.currentPage, this.pagesize)
      },
    },
  }
</script>

<style lang="scss">
  @import '@/ve/elements/basic/uiTable/style.scss';
  @import '@/ve/styles/elements.scss';

  .custom-report {
    padding: 20px;
    height: calc(100vh - 58.8px);
    position: relative;
    .steps {
      position: absolute;
      top: 40px;
      width: calc(100% - 40px);
      background: white;
      z-index: 100;
      padding: 10px;
    }
    .dialogContent {
      margin-top: 55px;
      .vxe-table {
        height: 100%;
      }
    }
    .el-form {
      .el-form-item--small.el-form-item {
        margin-bottom: 0px;
        width: 200px;
      }
      .el-form-item--small.el-form-item {
        margin-bottom: 0px;
      }
      .el-input--small .el-input__inner {
        height: 26px;
      }
      .el-form-item--small .el-form-item__content {
        line-height: 26px;
      }
      .el-form-item--small .el-form-item__label {
        line-height: 26px;
      }
    }
    .mainTable {
      height: calc(100% - 123px);
    }
  }
  .custom-report-forms {
    display: flex;
    align-items: flex-end;
  }
  .operate {
    line-height: 30px;
  }
  .footer {
    text-align: center;
    margin-top: 5px;
    width: calc(100% - 10px);
  }
  .successSet {
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 104px;
    margin-top: 120px;
  }
  .successSet p {
    margin-top: 30px;
  }
</style>
